//
//  InteractionMenu.m
//  IntelligenceCommunity
//
//  Created by zhangpei on 16/1/5.
//  Copyright © 2016年 zhangpei. All rights reserved.
//

#import "InteractionMenu.h"
#import "UtilityConfig.h"
#import "ViewHelper.h"
#import "PublicMethod.h"

#define MENUHEIGHT 32

#define ITEMWIDTH 80

#define INTERACTIONBUTONHEIGHT 20

@interface InteractionMenu ()
{
    UIWindow *window;
    
    InteractionButton *praiseButton;
    InteractionButton *stepButton;
    InteractionButton *commentButton;
    InteractionButton *helpButton;
    InteractionButton *noIconButton;

}

@property (nonatomic, strong) UIView *menu;

@property (nonatomic, assign) InteractionMenuType interactionMenuType;

@property (nonatomic, assign) InteractionMenuAlreadyClickType interactionMenuAlreadyClickType;

@property (nonatomic, assign) InteractionMenuBiddingType interactionMenuBiddingType;


@property (nonatomic, assign) CGPoint rightPoint;

@end

static InteractionMenu *interactionMenu;

@implementation InteractionMenu

@synthesize interactionMenuBlock;

@synthesize menu;

@synthesize interactionMenuType;

@synthesize interactionMenuAlreadyClickType;

@synthesize interactionMenuBiddingType;

@synthesize rightPoint;

+ (id)sharedInteractionMenuWithPosition:(CGPoint)point withInteractionMenuType:(InteractionMenuType)type withInteractionMenuAlreadyClickType:(InteractionMenuAlreadyClickType)alreadyClickType withDropListViewBlock:(InteractionMenuBlock)block
{
    @synchronized (self)
    {
        //为了确保多线程情况下，仍然确保实体的唯一性
        if (interactionMenu == nil)
        {
            interactionMenu = [[InteractionMenu alloc] init];
        }
        
        interactionMenu.rightPoint = point;
        
        interactionMenu.interactionMenuType = type;
        
        interactionMenu.interactionMenuAlreadyClickType = alreadyClickType;
        
        interactionMenu.interactionMenuBiddingType = (int)NSNotFound;

        interactionMenu.interactionMenuBlock = [block copy];
        
        [interactionMenu loadMenu];
        
        CGRect rect = interactionMenu.menu.frame;
        
        [WINDOW addSubview:interactionMenu];

        interactionMenu.menu.frame = CGRectMake(point.x, rect.origin.y, 0, rect.size.height);
       
        interactionMenu.hidden = NO;
        
        [UIView animateWithDuration:0.35 animations:^
        {
            interactionMenu.menu.frame =  rect;
        }
        completion:^(BOOL finished)
        {
           
        }];
        
        return interactionMenu;
    }
}

+ (id)sharedInteractionMenuWithPosition:(CGPoint)point withInteractionMenuType:(InteractionMenuType)type withInteractionMenuBiddingType:(InteractionMenuBiddingType)biddingType withDropListViewBlock:(InteractionMenuBlock)block
{
    @synchronized (self)
    {
        //为了确保多线程情况下，仍然确保实体的唯一性
        if (interactionMenu == nil)
        {
            interactionMenu = [[InteractionMenu alloc] init];
        }
        
        interactionMenu.rightPoint = point;
        
        interactionMenu.interactionMenuType = type;
        
        interactionMenu.interactionMenuAlreadyClickType = (int)NSNotFound;
        
        interactionMenu.interactionMenuBiddingType = biddingType;

        interactionMenu.interactionMenuBlock = [block copy];
        
        [interactionMenu loadMenu];
        
        CGRect rect = interactionMenu.menu.frame;
        
        interactionMenu.menu.frame = CGRectMake(point.x, rect.origin.y, 0, rect.size.height);
        
        [WINDOW addSubview:interactionMenu];
        
        interactionMenu.hidden = NO;

        [UIView animateWithDuration:0.35 animations:^
         {
             interactionMenu.menu.frame =  rect;
         }
                         completion:^(BOOL finished)
         {
             
         }];
        
        return interactionMenu;
    }
}


+ (id)allocWithZone:(NSZone *)zone
{
    @synchronized(self)
    {
        if (!interactionMenu)
        {
            interactionMenu = [super allocWithZone:zone]; //确保使用同一块内存地址
            return interactionMenu;
        }
        return nil;
    }
}

- (instancetype)init
{
    self = [super init];
    
    if (self)
    {
        self.backgroundColor = [UIColor clearColor];
        
        self.hidden = YES;
        
        [self initView];
    }
    return self;
}

- (void)loadMenu
{
    //清除已存在菜单
    for (UIView *view in menu.subviews)
    {
        [view removeFromSuperview];
    }
    
    //创建按钮
    [self createView];
    
    //根据菜单类型设置菜单属性
    switch (self.interactionMenuType)
    {
        case InteractionMenuTypeOfPraiseStepComment:
            {
                praiseButton.frame = CGRectMake(0, 0, ITEMWIDTH, MENUHEIGHT);
                praiseButton.tag = InteractionMenuClickTypeOfPraise;
                [menu addSubview:praiseButton];
                
                [self addSeperatedLineWithFrame:CGRectMake(ITEMWIDTH, 3, 2, MENUHEIGHT - 6)];
                
                stepButton.frame = CGRectMake(ITEMWIDTH + 2, 0, ITEMWIDTH, MENUHEIGHT);
                stepButton.tag = InteractionMenuClickTypeOfStep;
                [menu addSubview:stepButton];
                
                [self addSeperatedLineWithFrame:CGRectMake(ITEMWIDTH * 2 + 2, 3, 2, MENUHEIGHT - 6)];
                
                commentButton.frame = CGRectMake((ITEMWIDTH + 1) * 2, 0, ITEMWIDTH, MENUHEIGHT);
                commentButton.tag = InteractionMenuClickTypeOfComment;
                [menu addSubview:commentButton];
                
                menu.frame = CGRectMake(self.rightPoint.x - (ITEMWIDTH*3 + 4), self.rightPoint.y + (INTERACTIONBUTONHEIGHT - MENUHEIGHT)/2, ITEMWIDTH*3 + 4, MENUHEIGHT);
            }
            break;
            
        case InteractionMenuTypeOfPraiseComment:
            {
                praiseButton.frame = CGRectMake(0, 0, ITEMWIDTH, MENUHEIGHT);
                praiseButton.tag = InteractionMenuClickTypeOfPraise;
                [menu addSubview:praiseButton];
                
                [self addSeperatedLineWithFrame:CGRectMake(ITEMWIDTH, 3, 2, MENUHEIGHT - 6)];
                
                commentButton.frame = CGRectMake(ITEMWIDTH + 2, 0, ITEMWIDTH, MENUHEIGHT);
                commentButton.tag = InteractionMenuClickTypeOfComment;
                [menu addSubview:commentButton];
                
                menu.frame = CGRectMake(self.rightPoint.x - (ITEMWIDTH*2 + 2), self.rightPoint.y + (INTERACTIONBUTONHEIGHT - MENUHEIGHT)/2, (ITEMWIDTH*2 + 2), MENUHEIGHT);
            }
            break;
        case InteractionMenuTypeOfHelp:
            {
                helpButton.frame = CGRectMake(0, 0, ITEMWIDTH, MENUHEIGHT);
                helpButton.tag = InteractionMenuClickTypeOfHelp;
                [menu addSubview:helpButton];
                menu.frame = CGRectMake(self.rightPoint.x - ITEMWIDTH, self.rightPoint.y + (INTERACTIONBUTONHEIGHT - MENUHEIGHT)/2, ITEMWIDTH, MENUHEIGHT);
            }
            break;
        case InteractionMenuTypeOfBiddingComment:
            {
                noIconButton.frame = CGRectMake(0, 0, ITEMWIDTH, MENUHEIGHT);
                [menu addSubview:noIconButton];
                
                [self addSeperatedLineWithFrame:CGRectMake(ITEMWIDTH, 3, 2, MENUHEIGHT - 6)];
                
                commentButton.frame = CGRectMake(ITEMWIDTH + 2, 0, ITEMWIDTH, MENUHEIGHT);
                commentButton.tag = InteractionMenuClickTypeOfComment;
                [menu addSubview:commentButton];
                
                menu.frame = CGRectMake(self.rightPoint.x - (ITEMWIDTH*2 + 2), self.rightPoint.y + (INTERACTIONBUTONHEIGHT - MENUHEIGHT)/2, (ITEMWIDTH*2 + 2), MENUHEIGHT);
            }
            break;
        case InteractionMenuTypeOfComment:
            {
                commentButton.frame = CGRectMake(0, 0, ITEMWIDTH, MENUHEIGHT);
                commentButton.tag = InteractionMenuClickTypeOfComment;
                [menu addSubview:commentButton];
                menu.frame = CGRectMake(self.rightPoint.x - ITEMWIDTH, self.rightPoint.y + (INTERACTIONBUTONHEIGHT - MENUHEIGHT)/2, ITEMWIDTH, MENUHEIGHT);
            }
            break;
            
        default:
            break;
    }

    //如果已经赞，设置赞按钮已选中
    if (self.interactionMenuAlreadyClickType == InteractionMenuAlreadyClickTypeOfPraise)
    {
        [praiseButton setState:InteractionButtonStateSelected];
        praiseButton.tag = InteractionMenuClickTypeOfCancelPraise;
    }
    else
    {
        [praiseButton setState:InteractionButtonStateNormal];
        praiseButton.tag = InteractionMenuClickTypeOfPraise;
    }
    
    //如果已经踩，设置踩按钮已选中
    if (self.interactionMenuAlreadyClickType == InteractionMenuAlreadyClickTypeOfStep)
    {
        [stepButton setState:InteractionButtonStateSelected];
        stepButton.tag = InteractionMenuClickTypeOfCancelStep;
    }
    else
    {
        [stepButton setState:InteractionButtonStateNormal];
        stepButton.tag = InteractionMenuClickTypeOfStep;
    }
    
    if (self.interactionMenuBiddingType != NSNotFound)
    {
        switch (self.interactionMenuBiddingType)
        {
            case InteractionMenuBiddingTypeOfBidding:
                {
                    noIconButton.tag = InteractionMenuClickTypeOfBidding;
                    [noIconButton showNoIconButtonWithTitle:@"出价"];
                }
                break;
                
            case InteractionMenuBiddingTypeOfPayment:
                {
                    noIconButton.tag = InteractionMenuClickTypeOfPayment;
                    [noIconButton showNoIconButtonWithTitle:@"付款信息"];
                }
                break;
                
            case InteractionMenuBiddingTypeOfMail:
                {
                    noIconButton.tag = InteractionMenuClickTypeOfMail;
                    [noIconButton showNoIconButtonWithTitle:@"发货信息"];
                }
                break;
                
            case InteractionMenuBiddingTypeOfReceiving:
                {
                    noIconButton.tag = InteractionMenuClickTypeOfReceiving;
                    [noIconButton showNoIconButtonWithTitle:@"确认收货"];
                }
                break;
                
            default:
                break;
        }
    }
}


- (void)initView
{
    window = [[[UIApplication sharedApplication] windows] objectAtIndex:0];
    self.frame = window.bounds;
    [window addSubview:self];
    
    //透明覆层
    UIImageView *clearImageView = [[UIImageView alloc] initWithFrame:self.bounds];
    clearImageView.backgroundColor = [UIColor clearColor];
    [self addSubview:clearImageView];
    
    [self createView];
}

- (void)createView
{
    [self createMenu];
    
    [self createPraiseButton];
    
    [self createStepButton];
    
    [self createCommentButton];
    
    [self createHelpButton];
    
    [self createNoIconButton];
}

- (void)addSeperatedLineWithFrame:(CGRect)frame
{
    [ViewHelper superView:menu withFrame:frame withImage:nil withBackgroundColor:COLORRGBA(38, 42, 43, 1) withUserInteractionEnabled:NO];
}

- (void)createMenu
{
    if (menu == nil)
    {
        menu = [[UIView alloc] initWithFrame:CGRectMake(0, 0, MENUHEIGHT, MENUHEIGHT)];
        
        menu.backgroundColor = COLORRGBA(53, 61, 59, 1);
        
        menu.layer.cornerRadius = 4;
        
        menu.clipsToBounds = YES;
        
        [self addSubview:menu];
    }
}

- (void)createPraiseButton
{
    if (praiseButton == nil)
    {
        //赞
        NSArray *praiseTitles = @[@"赞", @"取消赞"];
        NSArray *praiseTitleColors = @[[UIColor whiteColor],COLORRGBA(0, 119, 217, 1)];
        NSArray *praiseImages = @[@"interaction_image_praise", @"interaction_image_praise_selected"];
        praiseButton = [self createButtonWithTitles:praiseTitles withTitleColors:praiseTitleColors withImages:praiseImages];
    }
    
}

- (void)createStepButton
{
    if (stepButton == nil)
    {
        //踩
        NSArray *stepTitles = @[@"踩", @"取消踩"];
        NSArray *stepTitleColors = @[[UIColor whiteColor],COLORRGBA(0, 119, 217, 1)];
        NSArray *stepImages = @[@"interaction_image_step", @"interaction_image_step_selected"];
        stepButton = [self createButtonWithTitles:stepTitles withTitleColors:stepTitleColors withImages:stepImages];
    }
}

- (void)createCommentButton
{
    if (commentButton == nil)
    {
        //评论
        NSArray *commentTitles = @[@"评论", @"评论"];
        NSArray *commentTitleColors = @[[UIColor whiteColor],[UIColor whiteColor]];
        NSArray *commentImages = @[@"interaction_image_comment", @"interaction_image_comment"];
        commentButton = [self createButtonWithTitles:commentTitles withTitleColors:commentTitleColors withImages:commentImages];
    }
}

- (void)createHelpButton
{
    if (helpButton == nil)
    {
        //提供帮助
        NSArray *helpTitles = @[@"提供帮助", @"提供帮助"];
        NSArray *helpTitleColors = @[[UIColor whiteColor],[UIColor whiteColor]];
        NSArray *helpImages = @[@"interaction_image_comment", @"interaction_image_comment"];
        helpButton = [self createButtonWithTitles:helpTitles withTitleColors:helpTitleColors withImages:helpImages];
    }
}

- (void)createNoIconButton
{
    if (noIconButton == nil)
    {
        //提供帮助
        NSArray *noIconTitles = @[@"X", @"X"];
        NSArray *noIconTitleColors = @[[UIColor whiteColor],[UIColor whiteColor]];
        NSArray *noIconImages = @[@"interaction_image_comment", @"interaction_image_comment"];
        noIconButton = [self createButtonWithTitles:noIconTitles withTitleColors:noIconTitleColors withImages:noIconImages];
    }
}



- (InteractionButton *)createButtonWithTitles:(NSArray *)titles withTitleColors:(NSArray *)colors withImages:(NSArray *)images
{
    
    InteractionButton *button = [[InteractionButton alloc] initWithFrame:CGRectMake(0, 0, ITEMWIDTH, MENUHEIGHT) withTitles:titles withTitleColors:colors withImages:images];
    
    [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    
    return button;
}

- (void)buttonClick:(UIButton *)button
{
    [self hiddenInteractionMenu];

    if (interactionMenuBlock)
    {
        interactionMenuBlock((int)button.tag);
    }
}


- (void)hiddenInteractionMenu
{
    CGRect rect = self.menu.frame;
    
    [UIView animateWithDuration:0.35 animations:^
     {
        self.menu.frame = CGRectMake(rect.origin.x + rect.size.width, rect.origin.y, 0, rect.size.height);
     }
     completion:^(BOOL finished)
     {
         self.hidden = YES;
     }];
}



- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self hiddenInteractionMenu];
}



@end
